package com.moonshineproject.editor
{
	import com.moonshineproject.text.TextEditor;
	import com.moonshineproject.view.IContentWindow;
	
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	
	import mx.containers.Canvas;
	import mx.events.FlexEvent;
	import mx.managers.IFocusManagerComponent;

	public class BasicTextEditor extends Canvas implements IContentWindow, IFocusManagerComponent
	{
		protected var editor:TextEditor;
		protected var file:File;
		protected var created:Boolean = false;
		protected var loadingFile:Boolean = false;
		protected var savedText:String = "";
		protected var tempScrollTo:int = -1;

		private var tempSaveAs:File;

		override public function get label():String
		{
			if (!file)
				return "New";
			return file.name;
		}

		public function get longLabel():String
		{
			if (!file) 
				return "New";
			return file.nativePath;	
		}

		public function get currentFile():File
		{
			return file;
		}

		public function get text():String
		{
			return editor.dataProvider;
		}

		public function set text(v:String):void
		{
			editor.dataProvider = v;
		}

		public function isEmpty():Boolean
		{
			if (!file && text == "")
				return true;
			return false;
		}

		public function isChanged():Boolean
		{
			return savedText != text;
		}

		public function getEditorComponent():TextEditor
		{
			return editor;
		}

		public function BasicTextEditor()
		{
			super();
			percentHeight = 100;
			percentWidth = 100;
			addEventListener(FlexEvent.CREATION_COMPLETE, createdHandler);

			editor = new TextEditor();
			editor.percentHeight = 100;
			editor.percentWidth = 100;

			text = "";
		}

		override public function setFocus():void
		{
			if (editor)
				editor.setFocus();
		}

		override protected function createChildren():void
		{
			addChild(editor);

			super.createChildren();
		}

		protected function createdHandler(e:Event):void
		{
			created = true;
			if (file)
				callLater(open, [file]);
		}

		public function scrollTo(line:int):void
		{
			if (loadingFile)
			{
				tempScrollTo = line;
			}
			else
			{
				editor.scrollTo(line);
				editor.selectLine(line);
			}
		}

		public function open(newFile:File):void
		{
			loadingFile = true;
			file = newFile;
			if (!created)
				return;
			file.addEventListener(Event.COMPLETE, openHandler);
			file.load();
		}

		protected function openHandler(event:Event):void
		{
			loadingFile = false;
			// Get data from file
			savedText = file.data.toString();
			text = savedText;
			
			if (tempScrollTo > 0)
			{
				scrollTo(tempScrollTo);
				tempScrollTo = -1;
			}
			
			file.removeEventListener(Event.COMPLETE, openHandler);
		}

		public function save():void
		{
			if (!file)
			{
				saveAs();
				return;
			}

			var fw:FileStream = new FileStream();

			fw.open(file, FileMode.WRITE);
			// Store saved text after open call, in case it fails
			savedText = text;
			// Does not prefix with a 16-bit length word like writeUTF() does
			fw.writeUTFBytes(savedText);
			fw.close();
		}

		public function saveAs(file:File=null):void
		{
			if (file)
			{
				this.file = file;
				save();
				// Update labels
				dispatchEvent(new Event('labelChanged'));
				return;
			}

			tempSaveAs = new File();
			tempSaveAs.addEventListener(Event.SELECT, handleSaveAsSelect);
			tempSaveAs.addEventListener(Event.CANCEL, removeTempSaveAs);
			tempSaveAs.browseForSave('Save As');
		}

		protected function handleSaveAsSelect(event:Event):void
		{
			saveAs(tempSaveAs);
			removeTempSaveAs(event);
		}

		protected function removeTempSaveAs(event:Event):void
		{
			tempSaveAs.removeEventListener(Event.SELECT, handleSaveAsSelect);
			tempSaveAs.removeEventListener(Event.CANCEL, removeTempSaveAs);
			tempSaveAs = null;
		}

	}
}